 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=177 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:03 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Pre-populate forms
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='Prepopulate,validate,validation,JSP,servlet,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Pre-populate forms</div>

<div class='main-body'>
 
<br>Pre-populating form elements is a very common and important task in
a web application. If pre-population is implemented effectively, then a
web application becomes both simpler and more robust.

<P>Forms are used for two kinds of operations: editing the database, and
defining search criteria.

<p>When editing the database, there are two use cases for forms :
<ul>
<li>
the "add" use case, where a new record is added to the datastore - here,
input items are initially blank, or have default initial values set in
their static HTML</li>

<li>
the "change" use case, where an already existing record is to be edited
by the user - here, input items are <em>not</em> initially blank, but are dynamically
pre-populated with the current values fetched from the database</li>
</ul>

<P>In both cases, the user makes entries in the form, and then submits it for processing, using a <tt>POST</tt>.
If an error is detected, then there is a "return to sender" operation, and
the HTML form is redisplayed, with all its items dynamically pre-populated,
showing their most recently entered values. Appropriate error messages are also
displayed.

<P>(For forms used for input of search criteria, the behavior is similar to the "add" use case 
described above, with the important difference that the form uses <tt>GET</tt> instead of <tt>POST</tt>.)

<p>How can this behavior be implemented most effectively?
<p>One method uses a single custom 
<a href='http://www.web4j.com/web4j/javadoc/hirondelle/web4j/ui/tag/Populate.html'><tt>Populate</tt></a> tag, 
and is <span class='highlight'>guided by the aesthetic ideal that adding pre-population to a form should have
an absolutely minimal affect on the markup used in the case of a regular, static HTML form</span>.
This seems very effective, since
<ul>
<li>
web page authors do not need to learn anything new, since <i>forms remain
almost exactly as they are in the static case</i></li>

<li>
it radically minimizes the effort needed to implement pre-population</li>

<li>
it reuses request parameters which are already present <i>and</i> already
in the form needed for ultimate display</li>

<li>
it couples HTML forms to the details of a domain object only through
a simple naming convention, which maps the names of input controls to the
<tt>getXXX</tt> methods of the domain object</li>

<li>
implementations of domain classes need to handle only the case of <i>valid</i>
data, so they become smaller and simpler, a <a href="TopicAction0d31-2.html">class
invariant</a> is easy to define, and they can be <a href='TopicActiond838-2.html'>immutable objects</a></li>
</ul>
An inferior style, commonly seen in frameworks such as Struts and Java
Server Faces, replaces the standard HTML tags such as <tt>&lt;INPUT></tt>
and
<tt>&lt;SELECT></tt> with a parallel collection of custom tags. Such
a style has these disadvantages :
<ul>
<li>
the form needs radical alteration from the style used in a simple, static
HTML form</li>

<li>
any prototype written in HTML must be either discarded or altered radically</li>

<li>
the web page author is forced to learn the details of multiple custom tags,
which are different for each framework</li>

<li>
if the framework is changed, then another set of custom tags must be learned</li>

<li>
pre-population is more of a programming task than a rendering task, but
in this style the page author is mainly responsible for its implementation</li>
</ul>

<h4>Example</h4>

<p>The <a href="TopicAction5f31-2.html">web4j</a> framework implements such a <tt><a href="http://www.web4j.com/web4j/javadoc/hirondelle/web4j/ui/tag/Populate.html">Populate</a></tt>
tag. Here, a JSP uses the <tt>&lt;w:populate></tt> tag to wrap the controls of a &lt;FORM&gt; tag that may need dynamic population.
Note how the <tt>&lt;w:populate></tt> tag simply wraps the body of the form, and represents an absolutely minimal
change to the style used in a simple, static HTML form.

<PRE>
&lt;w:populate using="itemForEdit"&gt;
 &lt;form action='MemberAction.do' method="post"&gt;
  &lt;input name="Id" type="hidden"&gt;
  &lt;table align="center"&gt;
   &lt;tr&gt;
    &lt;td&gt;&lt;label&gt;Name&lt;/label&gt; *&lt;/td&gt;
    &lt;td&gt;&lt;input name="Name" type="text"&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
    &lt;td&gt;&lt;label&gt;Is Active?&lt;/label&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input name="Is Active" type="checkbox" value="true"&gt;&lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
    &lt;td&gt;&lt;label&gt;Disposition&lt;/label&gt;&lt;/td&gt;
    &lt;td&gt;
     &lt;select name="Disposition"&gt;
      &lt;option&gt; &lt;/option&gt;
      &lt;c:forEach var="item" items="${dispositions}"&gt;
        &lt;option value="${item.id}"&gt;${item}&lt;/option&gt;
      &lt;/c:forEach&gt;
     &lt;/select&gt;
    &lt;/td&gt;
   &lt;/tr&gt;
&lt;/w:populate&gt;

   &lt;tr&gt;
    &lt;td align="center" colspan=2&gt;
     &lt;input type="submit" value="add.edit.button"&gt;
    &lt;/td&gt;
   &lt;/tr&gt;
  &lt;/table&gt;
  &lt;tags:hiddenOperationParam/&gt;

&lt;/form&gt;
</PRE>

<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction159e-2.html?Id=175'>Prefer JSTL tags</a> <br>
 
  
  <a href='TopicAction5f31-2.html?Id=188'>A Web App Framework - WEB4J</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='177'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=177 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:04 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
